package com.adv.ems.filters.report.after;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.adv.ems.db.entity.EnergyDataCumulateReportDayDO;
import com.adv.ems.db.entity.EnergyDataCumulateReportHourDO;
import com.adv.ems.filters.EnergyDataBaseFilter;
import com.adv.ems.filters.dto.FilterParam;
import com.adv.ems.filters.report.dto.ReportHourPoolDTO;
import com.adv.ems.manager.EnergyDataCumulateReportDayManager;
import com.adv.ems.manager.EnergyDataCumulateReportHourManager;
import com.adv.ems.model.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.*;

@Slf4j
@Component
public class DayReportAfterFilter extends EnergyDataBaseFilter<ReportHourPoolDTO, Boolean> {
    @Resource
    EnergyDataCumulateReportHourManager energyDataCumulateReportHourManager;
    @Resource
    EnergyDataCumulateReportDayManager energyDataCumulateReportDayManager;

    @Override
    protected String getFilterName() {
        return "报表天数据统计";
    }

    @Override
    protected double getOrder() {
        return 2;
    }

    @Override
    protected boolean isBefore() {
        return false;
    }

    @Override
    protected String getType() {
        return ReportHourPoolDTO.class.getName();
    }

    @Override
    protected ResultVO<Boolean> handle(FilterParam<ReportHourPoolDTO> param) {
        Map<Long, Set<String>> colDayMap = new HashMap<>();
        param.getList().forEach(h -> {
            Long pointId = h.getPointId();
            String day = DateUtil.formatDate(h.getTime());
            Set<String> orDefault = colDayMap.getOrDefault(pointId, new HashSet<>());
            orDefault.add(day);
            colDayMap.put(pointId, orDefault);
        });
        for (Map.Entry<Long, Set<String>> entry : colDayMap.entrySet()) {
            Long pointId = entry.getKey();
            entry.getValue().forEach(d -> {
                rebuildDayReport(pointId, d);
            });
        }
        return ResultVO.success(true);
    }

    private void rebuildDayReport(Long pointId, String dayStr) {
        log.info("统计采集点ID【{}】统计时间：【{}】开始", pointId, dayStr);
        DateTime dateTime = DateUtil.parseDate(dayStr);
        Date start = dateTime.toJdkDate();
        Date end = new Date(start.getTime() + Duration.ofDays(1).toMillis() - 1);
        List<EnergyDataCumulateReportHourDO> dayDataList = energyDataCumulateReportHourManager.getAllDataByPonitTime(pointId, start, end);
        Double increaseRealValue = 0d;
        Double increaseStandValue = 0d;
        Double increaseCashValue = 0d;
        Long firstId = null;
        Double firstValue = null;
        Long lastId = null;
        Double lastValue = null;
        for (EnergyDataCumulateReportHourDO data : dayDataList) {
            if (firstId == null) {
                firstId = data.getFirstId();
                firstValue = data.getFirstRealValue();
            }
            lastId = data.getLastId();
            lastValue = data.getLastRealValue();
            increaseRealValue = data.getIncreaseRealValue() + increaseRealValue;
            increaseStandValue = data.getIncreaseStandValue() + increaseStandValue;
            increaseCashValue = data.getIncreaseCashValue() + increaseCashValue;
        }
        energyDataCumulateReportDayManager.deleteByPointColDate(pointId, start);
        EnergyDataCumulateReportDayDO reportDay = new EnergyDataCumulateReportDayDO();
        reportDay.setCollectPointId(pointId);
        reportDay.setFirstId(firstId);
        reportDay.setFirstRealValue(firstValue);
        reportDay.setLastId(lastId);
        reportDay.setLastRealValue(lastValue);
        reportDay.setIncreaseRealValue(increaseRealValue);
        reportDay.setIncreaseStandValue(increaseStandValue);
        reportDay.setIncreaseCashValue(increaseCashValue);
        reportDay.setColDate(start);
        reportDay.setCreateTime(new Date());
        energyDataCumulateReportDayManager.insertData(reportDay);
        log.info("统计采集点ID【{}】统计时间：【{}】结束", pointId, dayStr);
    }
}
